/**
 * Created by lxy_920912 on 27/03/2017.
 */
var countAndSay = function(n) {
    if(n == 1){
        return '1';
    }
    var strArray = [];
    strArray[0] = '1';
    for(var i = 1;i < n;i++){
        var oldStr = strArray.pop();
        var count = 1;
        var newStr = '';
        for(var j = 1;j < oldStr.length;j++){
            if(oldStr.charAt(j) == oldStr.charAt(j-1)){
                count++;
            }else{
                newStr = newStr+count+oldStr.charAt(j-1);
                count = 1;
            }
        }
        newStr = newStr+count+oldStr.charAt(j-1);
        strArray.push(newStr)
    }
    return strArray.pop();
};
console.log(countAndSay(4));